﻿using BCCommon;
using BCCommon.Enums;
using BCData.OA.WorkflowTemplate.WorkflowTemplateFormCustomDatabaseTable;
using BCDto.OA.WorkflowTemplate.WorkflowTemplateFormCustomDatabaseTable;
using BCEntity.OA.WorkflowTemplate.WorkflowTemplateFormCustomDatabaseTable;
using BCEntity.Common.EntityNameConst;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BCService.OA.WorkflowTemplate.WorkflowTemplateFormCustomDatabaseTable
{
	public class WorkflowTemplateFormCustomDatabaseTableService : IWorkflowTemplateFormCustomDatabaseTableService
	{
		private readonly IDatabaseContext databaseContext;
		private readonly IRedisService redisService;
		private readonly IWorkflowTemplateFormCustomDatabaseTableData workflowTemplateFormCustomDatabaseTableData;
		private readonly string key = string.Format("{0}", EntityNameConst.WorkflowTemplateFormCustomDatabaseTableEntity);

		public WorkflowTemplateFormCustomDatabaseTableService(IDatabaseContext databaseContext,
			IRedisService redisService,
			IWorkflowTemplateFormCustomDatabaseTableData workflowTemplateFormCustomDatabaseTableData)
		{
			this.databaseContext = databaseContext;
			this.redisService = redisService;
			this.workflowTemplateFormCustomDatabaseTableData = workflowTemplateFormCustomDatabaseTableData;
		}

		#region 检查工作流模板表单自定义数据库表集合是否存在
		public bool Exists(long workflowTemplateFormCustomDatabaseTableId)
		{
			return this.workflowTemplateFormCustomDatabaseTableData.Exists(workflowTemplateFormCustomDatabaseTableId).GetAwaiter().GetResult();
		}
		#endregion

		#region 添加工作流模板表单自定义数据库表集合
		public WorkflowTemplateFormCustomDatabaseTableDto Add(WorkflowTemplateFormCustomDatabaseTableRequestDto requestDto)
		{
			var entity = requestDto.As<WorkflowTemplateFormCustomDatabaseTableEntity>();

			var result = this.workflowTemplateFormCustomDatabaseTableData.Add(entity).GetAwaiter().GetResult().As<WorkflowTemplateFormCustomDatabaseTableDto>();
			this.redisService.DeleteMultipleKey(key, false);
			return result;
		}
		#endregion

		#region 修改工作流模板表单自定义数据库表集合
		public WorkflowTemplateFormCustomDatabaseTableDto Update(WorkflowTemplateFormCustomDatabaseTablePutDto putDto)
		{
			if (!this.Exists(putDto.WorkflowTemplateFormCustomDatabaseTableId))
			{
				throw new ArgumentException("此记录不存在!");
			}
			var entity = putDto.As<WorkflowTemplateFormCustomDatabaseTableEntity>();
			var result = this.workflowTemplateFormCustomDatabaseTableData.Update(entity).GetAwaiter().GetResult().As<WorkflowTemplateFormCustomDatabaseTableDto>();
			this.redisService.DeleteMultipleKey(key, false);
			return result;
		}
		#endregion

		#region 删除工作流模板表单自定义数据库表集合
		public bool Delete(long workflowTemplateFormCustomDatabaseTableId)
		{
			if (!this.Exists(workflowTemplateFormCustomDatabaseTableId))
			{
				throw new ArgumentException("此记录不存在!");
			}

			var result = this.workflowTemplateFormCustomDatabaseTableData.Delete(workflowTemplateFormCustomDatabaseTableId).GetAwaiter().GetResult();
			this.redisService.DeleteMultipleKey(key, false);
			return result;
		}
		#endregion

		#region 获取工作流模板表单自定义数据库表集合
		public WorkflowTemplateFormCustomDatabaseTableDto Get(long workflowTemplateFormCustomDatabaseTableId)
		{
			string cacheKey = this.redisService.GetKey(key, workflowTemplateFormCustomDatabaseTableId);
			var result = this.redisService.TryGet(cacheKey, () =>
			{
				var data = this.workflowTemplateFormCustomDatabaseTableData.GetEntity(workflowTemplateFormCustomDatabaseTableId).GetAwaiter().GetResult();
				return data;
			}, TimeSpan.FromDays(1));
			return result.As<WorkflowTemplateFormCustomDatabaseTableDto>();
		}
		#endregion

		#region 获取工作流模板表单自定义数据库表集合列表
		public IEnumerable<WorkflowTemplateFormCustomDatabaseTableDto> GetList(string formConfigCode)
		{
			string cacheKey = this.redisService.GetKey(key, formConfigCode);
			var result = this.redisService.TryGet(cacheKey, () =>
			{
				return this.workflowTemplateFormCustomDatabaseTableData.GetList(formConfigCode).GetAwaiter().GetResult();
			}, TimeSpan.FromDays(1));
			return result.As<IEnumerable<WorkflowTemplateFormCustomDatabaseTableDto>>();
		}
		#endregion

		#region 获取工作流模板表单自定义数据库表集合分页
		public PageableList<WorkflowTemplateFormCustomDatabaseTableDto> Query(DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
		{
			string cacheKey = this.redisService.GetKey(key, start, end, sortName, ascending, pageIndex, pageSize);
			var result = this.redisService.TryGet(cacheKey, () =>
			{
				var data = this.workflowTemplateFormCustomDatabaseTableData.Query(start, end, sortName, ascending, pageIndex, pageSize).GetAwaiter().GetResult();
				return new PageableList<WorkflowTemplateFormCustomDatabaseTableDto>
				{
					Count = data.Item2,
					Items = data.Item1.As<IEnumerable<WorkflowTemplateFormCustomDatabaseTableDto>>(),
					PageIndex = pageIndex,
					PageSize = pageSize,
					ServerTime = DateTime.Now
				};
			}, TimeSpan.FromMinutes(30));

			return result;
		}
		#endregion
	}
}